package hivi.vn.hivigold.fragment;

import java.util.ArrayList;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.adapters.GoldPriceAdapter;
import hivi.vn.hivigold.adapters.PriceLoader;
import hivi.vn.hivigold.database.Preferences;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Gold;
import hivi.vn.hivigold.utils.Formats;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GoldPriceFagment extends Fragment implements OnItemClickListener, LoaderCallbacks<ArrayList<Gold>>{
	GoldPriceAdapter adapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.goldprice_layout, null);
		ListView lv = (ListView)v.findViewById(R.id.lstGoldPrice);
		adapter = new GoldPriceAdapter(getActivity(), null);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		getLoaderManager().initLoader(0, null, this);
		return v;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub 
		if(Preferences.getPreferenceValue(getActivity(), Preferences.ACCESS).equals("*") || Preferences.getPreferenceValue(getActivity(), Preferences.ACCESS).equals("0"))
		{
			Toast.makeText(getActivity(),R.string.warning_access_denied, Toast.LENGTH_LONG).show();
			return;
		}
		View v = getActivity().getLayoutInflater().inflate(R.layout.price_layout,null);
		final TextView tvGoldName = (TextView)v.findViewById(R.id.tvPriceName);
		final EditText txtPriceBuy = (EditText)v.findViewById(R.id.txtBuy_price);
		final EditText txtPriceSale = (EditText)v.findViewById(R.id.txtSale_price);
		final Button btnSave = (Button)v.findViewById(R.id.btnSave);
		
		final Gold g = adapter.getGoldAtIndex(arg2);
		tvGoldName.setText(g.getTienteName());
		txtPriceBuy.setText(Formats.money(g.getMua_vao()));
		txtPriceSale.setText(Formats.money(g.getBan_ra()));
		txtPriceBuy.requestFocus();
		final Dialog dialog = new Dialog(getActivity());
		btnSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(txtPriceBuy.getText().toString().trim().length() == 0 || txtPriceSale.getText().toString().trim().length() == 0)
				{
					Toast.makeText(getActivity(), R.string.warning_price_notempty, Toast.LENGTH_LONG).show();
					return;
				}
				g.setBan_ra(Double.parseDouble(txtPriceSale.getText().toString().replace(",","")));
				g.setMua_vao(Double.parseDouble(txtPriceBuy.getText().toString().replace(",","")));
				g.setUser_id2(Preferences.getPreferenceValue(getActivity(), Preferences.CURRENTUSER));				
				dialog.dismiss();
				PriceTask pt = new PriceTask();
				pt.execute(g);
			}
		});
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setLayout(600, LayoutParams.WRAP_CONTENT);
		dialog.setContentView(v);
		dialog.show();
	}

	private class PriceTask extends AsyncTask<Gold, Void, Boolean>
	{
		ProgressDialog dialog;
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result)
			{
				Toast.makeText(getActivity(), R.string.alert_change_price_success, Toast.LENGTH_LONG).show();
				getLoaderManager().getLoader(0).onContentChanged();
			}else
			{
				Toast.makeText(getActivity(), R.string.alert_change_price_fail, Toast.LENGTH_LONG).show();
			}
			dialog.dismiss();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(getActivity());
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(Gold... g) {
			// TODO Auto-generated method stub
			return WebService.updateGoldPrice(getActivity(), g[0]);
		}
	}
	
	@Override
	public Loader<ArrayList<Gold>> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		return new PriceLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<ArrayList<Gold>> arg0,
			ArrayList<Gold> arg1) {
		// TODO Auto-generated method stub
		adapter.swaper(arg1);
	}

	@Override
	public void onLoaderReset(Loader<ArrayList<Gold>> arg0) {
		// TODO Auto-generated method stub
		adapter.swaper(null);
	}

	
}
